package cn.chenxiejia;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/***
 * @program: maven
 * @ClassName: File4
 * @Description: 统计文件种类数量
 * @Author: chenxiejia
 * @Date: 2022/1/18 14:51
 * @version : V1.0
 */
public class File4 {
    public static Map<String, Integer> map = new HashMap<>();

    public static void main(String[] args) {
        count("E:\\aa");
        System.out.println("文件种类有" + map.size() + "种");
        map.forEach((k, v) -> System.out.printf("%s = %d %n", k, v));
    }

    public static void count(String s) {
        count(new File(s));
    }

    public static void count(File s) {
        if (s.isDirectory()) {
            for (File t : s.listFiles()) {
                if (t.isDirectory()) {
                    count(t);
                } else {
                    String name = t.getName();
                    int p = name.lastIndexOf(".");
                    String suffix = p == -1 ? "未知" : name.substring(name.lastIndexOf("."));
                    if (map.containsKey(suffix)) {
                        map.put(suffix, map.get(suffix) + 1);
                    } else {
                        map.put(suffix, 1);
                    }
                }
            }
        }
    }
}
